package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.*;
import com.jiahao.material.mapper.WarehouseMaterialMapper;
import com.jiahao.material.services.IRoleService;
import com.jiahao.material.services.IWarehouseService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private WarehouseMaterialMapper warehouseMaterialMapper;

    /**
     * 添加
     * @param warehouse
     * @return
     * @throws Exception
     */
    @RequiresPermissions("addWarehouse")
    @PostMapping("/add")
    public Result add(@RequestBody Warehouse warehouse) throws Exception{

        //check
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",warehouse.getName());
        List<Warehouse> list = warehouseService.list(queryWrapper);
        if (list!=null && list.size()>0){
                return ResultUtil.fail("该仓库已存在!");
        }

        warehouse.setStatus("01");
        warehouseService.save(warehouse);
        return ResultUtil.success("添加仓库成功!");
    }

    /**
     * 根据id查询仓库
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Warehouse byId = warehouseService.getById(id);
        return ResultUtil.success("",byId);
    }

    /**
     * 根据仓库id和材料id获取库存
     * @param wid,mid
     * @return
     * @throws Exception
     */
    @GetMapping("/num/{wid}/{mid}")
    public Result getNum(@PathVariable String wid,@PathVariable String mid) throws Exception{
        QueryWrapper<WarehouseMaterial> warehouseMaterialQueryWrapper = new QueryWrapper<>();
        warehouseMaterialQueryWrapper.eq("wid",wid);
        warehouseMaterialQueryWrapper.eq("mid",mid);

        WarehouseMaterial warehouseMaterial = warehouseMaterialMapper.selectOne(warehouseMaterialQueryWrapper);
        return ResultUtil.success("",warehouseMaterial.getCount());
    }

    /**
     * 查询所有仓库
     * @return
     * @throws Exception
     */
    @GetMapping("/all")
    public Result getAll() throws Exception{
        List<Warehouse> list = warehouseService.getAll();
        return ResultUtil.success("",list);
    }

    /**
     * 查询所有正常仓库
     * @return
     * @throws Exception
     */
    @GetMapping("/activeAll")
    public Result getActiveAll() throws Exception{
        QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<Warehouse>();
        warehouseQueryWrapper.eq("status","01");
        List<Warehouse> list = warehouseService.list(warehouseQueryWrapper);
        return ResultUtil.success("",list);
    }

    /**
     * 校验名字
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/checkName")
    public Result checkName(@RequestBody Map<String,Object> map) throws Exception{
        QueryWrapper<Warehouse> permissionQueryWrapper = new QueryWrapper<>();
        permissionQueryWrapper.eq("name",map.get("name").toString());
        Warehouse one = warehouseService.getOne(permissionQueryWrapper);
        String e="0";
        if (one!=null)
            e = one.getId().equals(map.get("id").toString())?"0":"1";
        return ResultUtil.success(e);
    }

    /**
     * 分页查询
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Warehouse> page = new Page<>(currentPage,pageSize);

        IPage<Warehouse> byPage = warehouseService.getByPage(page, map);

        return ResultUtil.success("",byPage);

    }

    /**
     * 更新
     * @param warehouse
     * @return
     * @throws Exception
     */
    @RequiresPermissions("updateWarehouse")
    @PutMapping("/update")
    public Result update(@RequestBody Warehouse warehouse) throws Exception{

        //check
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",warehouse.getName());
        List<Warehouse> list = warehouseService.list(queryWrapper);
        if (list!=null && list.size()>0){
            if (list.get(0).getId().equals(warehouse.getId())){
                return ResultUtil.fail("该仓库已存在!");
            }
        }

        warehouseService.updateById(warehouse);
        return ResultUtil.success("更新仓库成功!");
    }



    /**
     * 更新状态
     * @param map
     * @return
     * @throws Exception
     */
    @RequiresPermissions("updateWarehouse")
    @PutMapping("/status")
    @Transactional
    public Result updateStatus(@RequestBody Map<String,Object> map) throws Exception{
        UpdateWrapper<Warehouse> warehouseUpdateWrapper = new UpdateWrapper<>();
        warehouseUpdateWrapper.eq("id",map.get("id").toString()).set("status",map.get("status").toString());
        warehouseService.update(warehouseUpdateWrapper);
        return ResultUtil.success("更新状态成功!");
    }

    @RequiresPermissions("delWarehouse")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable String id) throws Exception{
        warehouseService.removeById(id);
        return ResultUtil.success("删除仓库成功!");
    }

    @RequiresPermissions("delWarehouse")
    @PostMapping("/dels")
    public Result delByIds(@RequestBody List<String> ids)  throws Exception{
        warehouseService.removeByIds(ids);
        return ResultUtil.success("删除仓库成功!");
    }

}
